﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;
using WebUI.BL.Products;
using System.Data;

namespace WebUI.BL.ProductSizeBL
{
    public class ProductSizeBL
    {
        public void Insert(tbl_ProductSize productSize)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductSize newProductSize = new tbl_ProductSize()
                    {
                        Status=productSize.Status,
                        SizeNo=productSize.SizeNo,
                        SizeName=productSize.SizeName,
                        SizeDesc=productSize.SizeDesc,
                        CreatedUser=productSize.CreatedUser,
                        CreatedDate=productSize.CreatedDate
                    };
                    contextdb.tbl_ProductSizes.InsertOnSubmit(newProductSize);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductSize productSize)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateProductSize = contextdb.tbl_ProductSizes.SingleOrDefault(u => u.SizeNo == productSize.SizeNo);
                try
                {
                    if (updateProductSize != null)
                    {
                        updateProductSize.Status = productSize.Status;
                        updateProductSize.SizeNo = productSize.SizeNo;
                        updateProductSize.SizeName = productSize.SizeName;
                        updateProductSize.SizeDesc = productSize.SizeDesc;
                        updateProductSize.CreatedUser = productSize.CreatedUser;
                        updateProductSize.CreatedDate = productSize.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productSizeNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteProductSize = contextdb.tbl_ProductSizes.SingleOrDefault(u => u.SizeNo == productSizeNo);
                try
                {
                    if (deleteProductSize != null)
                    {
                        contextdb.tbl_ProductSizes.DeleteOnSubmit(deleteProductSize);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }

        // Show Product By Product.
        public List<MODEL_SP_FONT_PRODUCT_SIZE> Get_Font_ProductSize(int productno, int colorno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var query = from n in contextdb.SP_FONT_PRODUCT_SIZE(productno, colorno)
                            select new MODEL_SP_FONT_PRODUCT_SIZE
                            {
                                SizeNo = n.SizeNo,
                                SizeName = n.SizeName
                            };
                return query.ToList();
            }
        }

        // Show All Size.
        public DataTable Get_SizeAll()
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                DataTable dt = new DataTable();
                var query = contextdb.SP_FONT_SIZE();
                dt = query.ConvertToDataTable(record => new object[] { query });
                return dt;
            }
        }
    }
}
